package com.zzyl.nursing.controller.member;


import javax.servlet.http.HttpServletResponse;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.dto.TimeCountDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class ReservationController extends BaseController {
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询预约信息列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取预约信息列表")
    public TableDataInfo<List<Reservation>> list(Reservation reservation) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }



    /**
     * 获取预约信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取预约信息详细信息")
    public R<Reservation> getInfo(@ApiParam(value = "预约信息ID", required = true)
                                  @PathVariable("id") Long id) {
        return R.ok(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:add')")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody ReservationDto dto) {
        reservationService.add(dto);
        return success();
    }

    /**
     * 修改预约信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:edit')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改预约信息")
    public AjaxResult edit(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation) {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:reservation:remove')")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除预约信息")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        int count = reservationService.selectCancelledReservationCountByUserId(userId);
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public R<List<TimeCountDto>> getReservationCountByTime() {
        List<TimeCountDto> list = reservationService.selectReservationCountByTime();
        return R.ok(list);
    }

    /**
     * 分页查询预约信息
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约信息")
    public R<TableDataInfo<List<Reservation>>> page(
            String name,
            String mobile,
            Integer type,
            Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        Reservation reservation = new Reservation();
        reservation.setName(name);
        reservation.setMobile(mobile);
        reservation.setType(type);
        reservation.setStatus(status);

        startPage(pageNum, pageSize);
        List<Reservation> list = reservationService.selectReservationList(reservation);
        TableDataInfo<List<Reservation>> dataTable = getDataTable(list);
        return R.ok(dataTable);
    }

    /**
     * 取消预约
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public R<Void> cancelReservation(@ApiParam(value = "预约信息ID", required = true) @PathVariable Long id) {
        boolean result = reservationService.cancelReservation(id);
        return result ? R.ok() : R.fail("取消预约失败");
    }

}
